<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=UTF-8" isErrorPage="true"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page import="com.gitee.qdbp.able.utils.StringTools" %>
<%@ taglib prefix="base" uri="http://qdbp.gitee.io/tags/base/"%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
	<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
	<meta name="HandheldFriendly" content="true" />
	<meta name="MobileOptimized" content="320" />
	<meta name="relative-path" content="<base:url href='/' />" />
	<title>微信支付</title>
</head>
<body>
	<div class="prompt" style="text-align:center">支付跳转中...</div>
	<script src="<base:url href='assets/libs/jquery/jquery.1.11.1.min.js'/>" type="text/javascript"></script>
	<script src="//res.wx.qq.com/open/js/jweixin-1.0.0.js" type="text/javascript"></script>
	<script>
	$(function() {
		var data = <%=StringTools.toJsonString(request.getAttribute("pv"))%>;
		var onBridgeReady = function() {
			// https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
			WeixinJSBridge.invoke("getBrandWCPayRequest", data, function(res) {
				if (res.err_msg == "get_brand_wcpay_request:ok") {
					// 使用该方式判断前端返回,微信团队郑重提示: res.err_msg将在用户支付成功后返回ok, 但并不保证它绝对可靠。 
					$(".prompt").html("支付成功");
					setTimeout(function() { window.location.replace(data.returnUrl); }, 2000);
				} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
					$(".prompt").html("支付已取消");
					setTimeout(function() { window.history.back() }, 2000);
				} else {
					var text = "支付失败, 请联系管理员";
					if (res) {
						try {
							var details = JSON.stringify(res).replace(/([\{,])/g, "$1 ").replace(/([\}])/g, " $1");
							text += "<br><p style='color:#DDD;text-align:left'>"+details+"</p>";
						} catch (e) { }
					}
					$(".prompt").html(text);
					setTimeout(function() { window.history.back() }, 5000);
				}
			});
		};
		if (window.WeixinJSBridge) {
			onBridgeReady();
		} else {
			if (document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
			} else if (document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
				document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
			}
		}
	});
	</script>
</body>
</html>
